package com.oms.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;
import com.oms.bean.Notice;

public class NoticeDao {

	QueryRunner qr=null;
	public NoticeDao() {
		qr = new TxQueryRunner();
	}
	public void Add(Notice notice) throws SQLException {
		String sql = "insert into oms_notice(notice,content,publish_time) values(?,?,?)";
		qr.update(sql,notice.getNotice(),notice.getContent(),notice.getPublish_time());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from oms_notice where id=?";
		qr.update(sql,id);
	}

	public void Update(Notice notice) throws SQLException {
		String sql = "update oms_notice set notice=?,content=?,publish_time=? where id=?";
		qr.update(sql, notice.getNotice(),notice.getContent(),notice.getPublish_time(),notice.getId());
	}

	public Notice FindById(int id) throws SQLException {
		String sql = "select * from oms_notice where id=?";
		Notice notice = qr.query(sql, new BeanHandler<Notice>(Notice.class),id);
		return notice;

	}

	public List<Notice> FindByString(String str) throws SQLException {
		String sql="select * from oms_notice N where Concat(N.*) like '%"+str+"%'";
		List<Notice> notices = qr.query(sql, new BeanListHandler<Notice>(Notice.class));
		return notices;
		
	}

	public List<Notice> FindAll() throws SQLException {
		String sql="select * from oms_notice";
		List<Notice> notices = qr.query(sql, new BeanListHandler<Notice>(Notice.class));
		return notices;
	}

	public long NoticeCount() throws SQLException {
		String sql="select count(id) from oms_notice";
		return qr.query(sql, new ScalarHandler<Long>());
	}

}
